package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.PageResult;
import com.woniu.mapper.CardLeagueMapper;
import com.woniu.pojo.CardLeague;
import com.woniu.pojo.League;
import com.woniu.mapper.LeagueMapper;
import com.woniu.service.ILeagueService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.vo.LeagueDetailVo;
import com.woniu.vo.LeagueQueryVo;
import com.woniu.vo.LeagueSearchVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zbk
 * @since 2022-10-12
 */
@Service
public class LeagueServiceImpl extends ServiceImpl<LeagueMapper, League> implements ILeagueService {

    @Autowired(required = false)
    private LeagueMapper leagueMapper;
    @Autowired
    private CardLeagueMapper cardLeagueMapper;

    /**
     * 搜索 + 分页
     * @param currentPage
     * @param pageSize
     * @param leagueQueryVo
     * @return
     */
    public PageResult search(Integer currentPage, Integer pageSize, LeagueQueryVo leagueQueryVo) {
        IPage iPage = new Page(currentPage, pageSize);
        IPage<Map<String, Object>> leagueIPage = leagueMapper.selectSearch(iPage, leagueQueryVo);
        PageResult pageResult = new PageResult(leagueIPage.getTotal(), leagueIPage.getRecords());

        return pageResult;
    }

    /**
     * 查润所有团课名称
     * @return
     */
    public List<LeagueSearchVo> queryAll() {
        return leagueMapper.selectAll();
    }

    @Override
    public void addCardRelation(String leagueId, List<String> cardList) {
        QueryWrapper<CardLeague> wrapper =new QueryWrapper<>();
        wrapper.eq("league_id", leagueId);
        cardLeagueMapper.delete(wrapper);
        for (String cardId : cardList) {
            CardLeague cardLeague = new CardLeague();
            cardLeague.setCardId(cardId);
            cardLeague.setLeagueId(leagueId);
            cardLeagueMapper.insert(cardLeague);
        }
    }

}
